package com.gzhryc.shared_device.cdb.code.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdb.code.dao.CDBFeeTemplateDao;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBFeeTemplate;
import com.gzhryc.shared_device.cdb.code.services.dto.CDBFeeTemplateSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CDBFeeTemplateService {

    static Logger log = Logger.getLogger(CDBFeeTemplateService.class);

    public static CDBFeeTemplateService self() {
        return MultiDBTools.getService(CDBFeeTemplateService.class, true);
    }

    public static CDBFeeTemplateService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, CDBFeeTemplateService.class, true);
    }

    CDBFeeTemplateDao dao;

    public CDBFeeTemplateService(String jdbcKey) {
        dao = new CDBFeeTemplateDao(jdbcKey);
    }

    public List<CDBFeeTemplate> findJoinBySearch(CDBFeeTemplateSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findJoinByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countJoinBySearch(CDBFeeTemplateSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.countJoin(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<CDBFeeTemplate> findByUserId(Long userId) {
        if (userId != null) {
            Conditions conditions = new Conditions(CDBFeeTemplate.class);
            conditions.and().is("userId", userId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public CDBFeeTemplate getById(Long id) {
        if(id != null) {
            try {
                return dao.getById(id);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public CDBFeeTemplate getJoinById(Long id) {
        if(id != null) {
            try {
                return dao.getJoinById(id);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean add(CDBFeeTemplate feeTemplate)throws LogicException {
        if(feeTemplate.getUserId() == null){
            feeTemplate.setUserId(0L);  //不设置则是平台模版
        }
        feeTemplate.setCreateDate(new Date());
        try {
            return dao.insert(feeTemplate) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateNotNull(CDBFeeTemplate feeTemplate) throws LogicException {
        try {
            return dao.updateNotNull(feeTemplate) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    public boolean deleteById(Long id)throws LogicException{
        try {
            return dao.deleteById(id) >0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public static CDBFeeTemplate buildByDefault(CDBCostConfig config){
        CDBFeeTemplate freightBasis = new CDBFeeTemplate();
        freightBasis.setDepositMoney(config.getDepositMoney());
        freightBasis.setFreeTimes(config.getFreeTimes());
        freightBasis.setDayMaxMoney(config.getDayMaxMoney());
        freightBasis.setPayIntervalMoney(config.getPayIntervalMoney());
        return freightBasis;
    }
}
